/*
 * @lc app=leetcode.cn id=135 lang=golang
 *
 * [135] 分发糖果
 */

// @lc code=start
func candy(ratings []int) int {
	left := make([]int, len(ratings))
	for k, v := range left {
		left[k] = 1
	}
	right := make([]int, len(ratings))
	copy(right, left)

	//左规则
	for i := 1; i < len(ratings); i++ {
		if ratings[i] > ratings[i-1] {
			left[i] = left[i-1] + 1
		}
	}

	//右规则
	count := 0
	for i := len(ratings) - 2; i >= 0; i-- {
		if ratings[i] > ratings[i-1] {
			right[i] = right[i-1] + 1
		}
		count += max(right[i], left[i])
	}

	return count
}

func max(a,b int)int{
	if a>b{
		return a
	}else{
		retur b
	}
}

// @lc code=end

